package com.sinder.campus_prophet.service.jwgl.impl;

import cc.lyceum.api.thxy.jwgl.ThxyJwgl;
import cc.lyceum.api.thxy.jwgl.pojo.ExamArrange;
import cc.lyceum.api.thxy.jwgl.pojo.ExamResults;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinder.campus_prophet.core.enums.TimeBar;
import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.entity.jwgl.TbAchievement;
import com.sinder.campus_prophet.entity.jwgl.TbJwglExam;
import com.sinder.campus_prophet.mapper.jwgl.TbJwglInfoMapper;
import com.sinder.campus_prophet.service.jwgl.TbJwglExamService;
import com.sinder.campus_prophet.mapper.jwgl.TbJwglExamMapper;
import com.sinder.campus_prophet.service.jwgl.bean.dto.MyExamDto;
import com.sinder.campus_prophet.service.jwgl.bean.dto.MyExamSynDto;
import com.sinder.campus_prophet.service.jwgl.bean.vo.JwglInfoVo;
import com.sinder.campus_prophet.service.jwgl.bean.vo.MyExamVo;
import com.sinder.campus_prophet.utils.TermCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author AS
* @description 针对表【tb_jwgl_exam(教务系统个人考试信息)】的数据库操作Service实现
* @createDate 2023-06-13 22:25:20
*/
@Service
@Slf4j
public class TbJwglExamServiceImpl extends ServiceImpl<TbJwglExamMapper, TbJwglExam>
    implements TbJwglExamService{

    private final TbJwglInfoMapper jwglInfoMapper;

    private final TbJwglExamMapper jwglExamMapper;

    public TbJwglExamServiceImpl(TbJwglInfoMapper jwglInfoMapper, TbJwglExamMapper jwglExamMapper) {
        this.jwglInfoMapper = jwglInfoMapper;
        this.jwglExamMapper = jwglExamMapper;
    }

    /**
     * 查询个人考试信息
     * @param openId
     * @return
     */
    @Override
    public ResponseMessage getJwglMyExam(String openId) {
        JwglInfoVo jwglInfoVo = jwglInfoMapper.getJwglInfo(openId);
        if (jwglInfoVo == null) {
            return ResponseMessage.createByErrorMessage("用户不存在");
        }
        MyExamDto myExamDto = new MyExamDto();
        myExamDto.setOpenId(openId);
        myExamDto.setXnxqdm(TermCodeUtils.getCurrentCode(jwglInfoVo.getRxnf()));
        List<MyExamVo> myExamVoList = jwglExamMapper.getJwglExamList(myExamDto);
        if (myExamVoList.size() == 0) {
            return ResponseMessage.createByErrorMessage("暂无考试安排");
        }
        // 数据处理
        myExamVoList.forEach(item -> item.setDyxq(TimeBar.XQ[Integer.parseInt(item.getDyxq()) - 1]));
        return ResponseMessage.createBySuccess("考试信息", myExamVoList);
    }

    /**
     * 同步个人考试信息
     * @param myExamDto
     * @param jwgl
     * @return
     */
    @Override
    public ResponseMessage synMyExamData(MyExamSynDto myExamDto, ThxyJwgl jwgl) {
        String openId = myExamDto.getOpenId();
        // 数据同步开始时间
        long start = System.currentTimeMillis();
        // 1：登录教务系统
        try {
            // 3：获取考试信息 -- 数据同步前先清空相关用户旧数据
            QueryWrapper<TbJwglExam> jwglExamQuery = new QueryWrapper<>();
            jwglExamQuery.eq("open_id", openId);
            int achDelRow = jwglExamMapper.delete(jwglExamQuery);
            log.info("----已清空用户openId为：{}的考试信息{}条数据，正在同步新数据----", openId, achDelRow);
            // 根据入学年份获取四年学期参数 202001 == 大一(上) -- 入学年份插入教务信息表
            String rxnf = jwgl.getStudentInfo().getRxnf(); // 获取入学年份
            // 当前学期参数 因为考试信息只需要当前的才有用
            String currentCode = TermCodeUtils.getCurrentCode(rxnf);
            List<ExamArrange> examList = jwgl.getExamArrange(currentCode);
            // 有的人没有考试
            if (examList.size() > 0 ) {
                // 处理教务考试安排数据
                List<TbJwglExam> jwglExamList = examList.stream().map(item -> {
                    TbJwglExam tbJwglExam = new TbJwglExam();
                    BeanUtils.copyProperties(item, tbJwglExam);
                    tbJwglExam.setXqdm(currentCode);
                    tbJwglExam.setSynTime(new Date());
                    tbJwglExam.setOpenId(openId);
                    return tbJwglExam;
                }).collect(Collectors.toList());
                // 批量入库
                jwglExamMapper.insertBatch(jwglExamList);
            }
        } finally {
            // 结束后退出登录
            if (jwgl != null) {
                jwgl.logout();
            }
        }
        // 数据同步结束时间
        long end = System.currentTimeMillis();
        log.info("同步用户为：{},的考试信息，历时{}s", myExamDto.getOpenId(), (end-start) / 1000);
        return ResponseMessage.createBySuccessMessage("考试安排已同步");
    }
}




